import { BULLET_SPEED } from '../../../config/play_def';
import bullet from '../../base/bullet'
cc.Class({
    extends: bullet,

    properties: {
    },
    initInfo(param){
        this.script_name = `sword_bullet`    // 脚本名赋值 必填！
        this.attack_fly = param.attack_fly
        this.order_line = param.attack_line
        
        this.move_count = 0
        this.move_max = param.max_distance
        this.scale_sub = param.scale_sub
        this._super(param)
    },
    turnDir(){
        let t = this.dir.signAngle(cc.v2(1,0));
        if(t){
            var degree = t / Math.PI * 180 + 90;
            while (Math.abs(degree) > 360) {
                if(degree < 0){
                    degree += 360
                }else{
                    degree -= 360
                }
            }
            if(degree < 0){
                degree = 360 + degree
            }    
            this.node.angle = -degree
        }
    },
    // 清楚
    clearInfo(param){
        this.move_count = 0
        this.node.scale = 1
        this._super(param)  
    },
    // 攻击
    attack(monster){
        this._super(monster)  // 伤害变更
        if(this.collision_destory){
                this.first_collision = true
                this.move = false
                this.happenEffect(monster)
                this.playHit(monster)
        }else{
            this.playHit(monster)
            this.happenEffect(monster)
        }
    },
    checkMoveClear(res){
        this.move_count += res
        if( this.move_count > this.move_max){
            this.clearInfo()
        }
    },
    fly(dt){
        this._super(dt)
        let move_res = this.speed * BULLET_SPEED * dt 
        this.node.scale -= this.scale_sub * dt
        this.checkMoveClear(move_res)
        let target = this.dir.mul(move_res)
        this.node.position = target.add(this.node.position)        
    }
});
